home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 599 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  71 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!eskimo!scs
  3. From: scs@eskimo.com (Steve Summit)
  4. Subject: Re: mixing enums and integers
  5. X-Nntp-Posting-Host: eskimo.com
  6. Message-ID: <DKtpIL.72z@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: schmorganization
  9. References: <4cij6k$dvb@yrkpa.kias.com>
  10. Date: Sun, 7 Jan 1996 18:18:20 GMT
  11.  
  12. In article <4cij6k$dvb@yrkpa.kias.com>, glynis@yrkpa.kias.com
  13. (John Flinchbaugh) writes:
  14. > the faq states that mixing enums and ints is legal, but poor style.
  15.  
  16. Hm.  I can see how you might have interpreted it that way, but
  17. let me state for the record that that's not quite what the FAQ
  18. list is trying to say.
  19.  
  20. The actual wording is that indiscriminate mixing of enumerations
  21. and integers "can still be considered bad style."  If you believe
  22. that enumerations are an abstract type, distinct from plain
  23. integers, then you will not deliberately mix them in your code,
  24. and you will wish that correctness-checking tools would warn you
  25. when you do mix them (since under your style, such mixing is
  26. likely in error), and you may lament the ANSI C committee's
  27. decision that enumerations essentially *are* integers, thus
  28. blurring the distinction you'd like to observe.  If, on the other
  29. hand, your programming style is not so discriminatory between
  30. enumerations and integers, there's no reason you can't mix them
  31. freely, and the historical implementation of enumerations by
  32. compilers (as codified by the Standard) agrees with you.
  33. Finally, the FAQ list isn't even trying to insinuate that one
  34. approach is better than the other.  (I do tend to have my own
  35. opinions on these things, and I suppose I let them slip through
  36. in the FAQ list's wording more often than I ought to, but in this
  37. case, my opinion is not strong, and the issue isn't worth making
  38. a fuss over, so I really wasn't trying to polarize the discussion
  39. at all.)
  40.  
  41. Note also the insertion of that modifier "indiscriminate" in the
  42. FAQ list's wording.  Even if you do attempt to enforce a strict
  43. distinction between enumerations and integers in your code, the
  44. practical reality is that there are plenty of cases where an
  45. isolated assignment of an enumeration constant to an integer or
  46. an integer to an enumeration ends up being necessary.  I think
  47. the example posted by John Flinchbaugh probably falls into that
  48. category.
  49.  
  50. > would casting it be more proper:
  51. >     if ((enum DIRECT) key==up) ...
  52.  
  53. I don't think the cast buys you much stylistically, but it would
  54. tend to suppress warnings by compilers attempting to enforce a
  55. discipline which differentiates between enumerations and integers.
  56.  
  57. > also, can you typedef enums?  if so, how?
  58.  
  59. Certainly.
  60.  
  61.     typedef enum {...} enum_t;
  62. or
  63.     enum x {...};
  64.     typedef enum x enum_t;
  65. or
  66.     typedef enum x enum_t;
  67.     enum x {...};
  68.  
  69.                     Steve Summit
  70.                     scs@eskimo.com
  71.